package com.mfz.users

import com.mfz.common.definition.organization.Company
import com.mfz.common.persistence.AbstractFactory
import com.mfz.common.persistence.AbstractPersistence
import com.mfz.common.units.compareCurrent
import com.mfz.users.definition.EXPIRE_AT
import java.util.*

/**
 * 企业对象
 */
class Company private constructor() : AbstractPersistence(Companion), Company {
	private lateinit var _roles: List<Role>
	private lateinit var _departments: List<Department>
	private lateinit var _users: List<User>
	private lateinit var _permissin: List<Permission>


	override val roles: Collection<com.mfz.common.definition.organization.Role>
		get() {
			if (!this::_roles.isInitialized) {
				_roles = Role.loadCompanyRoles(id)
			}

			return _roles
		}
	override val permissions: Collection<com.mfz.common.definition.organization.Permission>
		get() {
			if (!this::_permissin.isInitialized) {
				_permissin = Permission.loadCompanyPermissions(id)
			}

			return _permissin
		}

	override val departments: Collection<com.mfz.common.definition.organization.Department>
		get() {
			if (!this::_departments.isInitialized) {
				_departments = Department.loadCompanyDepartments(id)
			}

			return _departments
		}


	override val users: Collection<com.mfz.common.definition.organization.User>
		get() {
			if (!this::_users.isInitialized) {
				_users = User.loadCompanyUsers(id)
			}

			return _users
		}
	override val isAvailable: Boolean
		get() = flag.notDelete && get<Date>(EXPIRE_AT)?.compareCurrent() ?: true

	companion object : AbstractFactory<com.mfz.users.Company>("company") {
		init {
			useBuffer = true
		}

		override fun createNewObject(): com.mfz.users.Company {
			return Company()
		}

	}
}
